<?php
/*
This file 'pps_teams.php' is part of the PPS project <http://code.google.com/p/fracsnetpps/>

Copyright: (C) 2009 Arthur, B. aka ]{ing <whinemore@gmail.com>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 .
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.
 .
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 .

*/
define('ALPHA', 1 );
define('BRAVO', 2 );
define('SPEC', 5 );

/* dragons */

class team{
    
    public $score;
    public $count;
    public $p;
    public $fc;
    public $flag;
    public $sigma;
    public $mu;
    public $number;
    
    public function __construct(){
        $this->score = 0;
        $this->count = 0;
        $this->flag = 1;
        $this->fc = null;
        $this->p = array();
    }
}

class teams_container{

    public $alpha;
    public $bravo;
    public $spec;

    private $ids;
    private $big_id;
    private $avid;
    private $sock;
    
    /* All players */
    public $ps;
    public $pc;
    
    private $balance_timeout;
    
    public function __construct(&$ssocket){
        
        $this->alpha = new team(); $this->alpha->number = ALPHA;
        $this->bravo = new team(); $this->bravo->number = BRAVO;
        $this->spec = new team();  $this->spec->number = SPEC;
        
        $this->sock = & $ssocket;
        
        $this->ps = array();
        $this->pc = 0;
        
        $this->big_id = 0;
        $this->avid = 1;
        for( $i = 0; $i < 32; $i++ ){
            $this->ids[$i] = 0;
            if( $i == 0 ) $this->ids[$i] = 1;
        }
        
        $this->can_balance = true;
    }

    public function shuffle(){/*
        $i = round($this->alpha->count/2, 0);
        
        $this->balance_timeout = $i * 2;
        $Ap = array_values($this->alpha->p);
        $Bp = array_values($this->bravo->p);
        fputs( $this->sock, "/say Shuffling teams in 8 seconds.\r\n" );
        sleep(8);
        for($k = 0; $k < $i; $k++){
            
            $shf_alpha = $this->ps[$Ap[$k*2]]->p_id;
            $shf_bravo = $this->ps[$Bp[$k*2]]->p_id;
            
            fputs( $this->sock, "/setteam2 $shf_alpha\r\n" );
            fputs( $this->sock, "/setteam1 $shf_bravo\r\n" );            
        } 
        fputs($this->sock, "/restart\r\n");
        fputs( $this->sock, "/say Teams shuffled, restarting...\r\n" ); */
        
    }
    
    public function balance(){
        /*
        if( $this->balance_timeout ){
            $this->balance_timeout--;
            return;
        }
        if( $this->alpha->count > $this->bravo->count ){
            $from = &$this->alpha;
            $to = &$this->bravo;
            $to_num = 2;
        }
        elseif( $this->bravo->count > $this->alpha->count ){
            $from = &$this->bravo;
            $to = &$this->alpha;
            $to_num = 1;
        }
       
        if( !isset($to_num) ) return;
        
        
        $diff = $from->count - $to->count;
        
        if( $diff > 1 )
        {
            fputs( $this->sock, "/say Balancing teams: ". $this->alpha->count . "v".$this->bravo->count ."\r\n" );
            $tc = $to->count;
            $fc = $from->count;
            do
            {
                $switch = mt_rand( 1, $this->big_id );
                if( !array_key_exists($switch, $from->p) ) continue;
                if( $from->p[$switch] == $from->fc ) continue;
                $tc++;
                $fc--;
                
                fputs( $this->sock, "/setteam$to_num $switch\r\n" );
                
                $diff = $fc - $tc;
                
            }while( $diff > 1 );
        }      */
    }
    
    public function add($name, $team, $id = null, $player = null){
        
        if( !isset($name) || !isset($team) ) return;
        $from = null;
        switch( $team ){
            case 1:
                $to = &$this->alpha;
                break;
            case 2:
                $to = &$this->bravo;
                break;
            case 5:
                $to = &$this->spec;
                break;
        }
        
        if( array_key_exists($name, $this->ps) ){
            $this->ps[$name]->team->sigma -= $this->ps[$name]->sigma;
            $this->ps[$name]->team->mu -= $this->ps[$name]->mu;
            
            $this->ps[$name]->team->count--;
            
            unset($this->ps[$name]->team->p[$id]);
            ksort($this->ps[$name]->team->p);
            
            $this->ps[$name]->team = &$to;
        }else{
            $this->ps[$name] = $player;
            $this->ps[$name]->team = &$to;
            if( $player ) $this->ps[$name]->p_id = ( $id ) ? $id : $this->avid;
            $this->pc++;
        }
 
        if( $id )
        {
            
            $to->p[$id] = $name;
            $to->count++;    
            $this->ids[$id]  = 1;
            if( $id > $this->big_id ) $this->big_id = $id;
            if( $this->avid == $id ) $this->avid = 0;
            
            $to->sigma += $this->ps[$name]->sigma;
            $to->mu += $this->ps[$name]->mu;
            
        }
                
        if( !$this->avid ){
            $this->avid = array_search( 0, $this->ids );
        }
        
    }
    
    public function remove($name, $team = null){
        
        if( !isset($name) ) return;
        
        if( !$team ){
            if( array_search( $name, $this->alpha->p ) )
                $team = 1;
            elseif( array_search( $name, $this->bravo->p ) )
                $team = 2;
            elseif( array_search( $name, $this->spec->p ) )
                $team = 5;
        }
        if( !$team ) return;
        
        switch( $team ){
            case 1:
                $from = &$this->alpha;
                break;
            case 2:
                $from = &$this->bravo;
                break;
            case 5:
                $from = &$this->spec;
                break;
        }
        
        foreach( $from->p as $key => $player )
        {
            if( $from->p[$key] == $name ){
                $this->ids[$key] = 0;
                if( $key < $this->avid ) $this->avid = $key;
                
                if( $key == $from->fc ) $from->fc = "";
                
                unset($from->p[$key]);
                ksort($from->p);
                
                unset($this->ps[$name]);
                ksort($this->ps);
                
                $this->pc--;
                
                $from->count--;
                
                break;
            }
        }
        
        $this->balance();
    }
}

